/*
 * ThreadSafeDataManager.h
 *
 *	线程安全的数据集合，可以添加一个读任务，获取一个读任务
 *	添加一个写socket任务，获取一个写sockt任务
 *  Created on: 2014-1-7
 *      Author: root
 */

#ifndef THREADSAFEDATAMANAGER_H_
#define THREADSAFEDATAMANAGER_H_

#include "../common/ClientClass.h"
#include "../common/UserInfo.h"

#include <map>
#include <vector>
#include <string>
#include <queue>
#include <pthread.h>
#include<semaphore.h>

namespace miniGameServer {

class ThreadSafeDataManager {
public:
	ThreadSafeDataManager();
	virtual ~ThreadSafeDataManager();
	/*
	 * 客户端列表
	 */
	std::map<int,ClientClass*> allClients;


private:
	/*
	 * 刚读入数据到客户端,可以交给逻辑服务器处理
	 */
	std::queue<ClientClass*> readClients;
	pthread_mutex_t readClientMutex;

	sem_t readListSem;
public:
	bool addOneReadClient(ClientClass *cc);
	/*
	 * 将队列中到第一个取出来。如果队列中没有，则返回NULL
	 */
	ClientClass * getOneReadClient();
	bool readListEmpty();
	void waitReadSem();
	void postReadSem();

private:
	/*
	 * 刚产生数据的客户端列表，网络线程可以发送数据了
	 */
	std::queue<ClientClass*> writeClients;
	pthread_mutex_t writeClientsMutex;
	sem_t writeListSem;
public:
	bool addOneWriteclient(ClientClass*cc);
	ClientClass *getOneWriteClient();
	bool writeClientEmpty();
	void waitWriteSem();
	void postWriteSem();
public:

	static miniGameServer::ThreadSafeDataManager& instance();
};

}

#endif /* THREADSAFEDATAMANAGER_H_ */
